package com.yanmaohu.nikita.analyzer;

import com.yanmaohu.nikita.NikitaException;
import com.yanmaohu.nikita.data.Type;
import com.yanmaohu.nikita.data.TypeList;
import com.yanmaohu.nikita.data.TypeModifier;
import com.yanmaohu.nikita.data.expression.NameExpression;
import com.yanmaohu.nikita.scanner.SourceCode;
import com.yanmaohu.nikita.utility.FileUtility;

/**
 * @author Bright Lee
 */
public class TypeNameAnalyzer {

    private final SourceCode sourceCode;
    private final TypeList typeList;

    public TypeNameAnalyzer(SourceCode sourceCode, TypeList typeList) {
        this.sourceCode = sourceCode;
        this.typeList = typeList;
    }

    public void analyze() {
        String fileName = FileUtility.getTypeName(typeList.getFile());
        for (int i = 0; i < typeList.size(); i++) {
            Type type = typeList.get(i);
            TypeModifier typeModifier = type.getTypeModifier();
            NameExpression typeName = type.getTypeName();
            if (!fileName.equals(typeName.getContent())) {
                if (typeModifier != null) {
                    if (typeModifier.isProtected()) {
                        throw new NikitaException(sourceCode, typeName.getBeginIndex(), typeName.getEndIndex(), "Remove protected modifier of this type.");
                    }
                    if (typeModifier.isPublic()) {
                        throw new NikitaException(sourceCode, typeName.getBeginIndex(), typeName.getEndIndex(), "Remove public modifier of this type.");
                    }
                }
            }
        }
    }

}
